package com.sunny.l76;

public class Solution {
    public void setZeroes(int[][] matrix) {
        if(null == matrix)
            return;
        int n = matrix.length;
        if(0 == n)
            return;
        int m = matrix[0].length;
        boolean col = false;
        boolean row = false;
        for(int i=0;i<n;i++){
            if(0 == matrix[i][0]){
                col = true;
            }
        }
        for(int j=0;j<m;j++){
            if(0 == matrix[0][j])
                row = true;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(matrix[i][j] == 0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        for(int i=1;i<n;i++){
            for(int j=1;j<m;j++){
                if(0 == matrix[0][j] || 0 == matrix[i][0])
                    matrix[i][j] = 0;
            }
        }
        if(col) {
            for (int i = 0; i < n; i++) {
                matrix[i][0] = 0;
            }
        }
        if(row){
            for(int j=0;j<m;j++){
                matrix[0][j] = 0;
            }
        }
    }
}